package com.sinosoft.cloud.access;

import com.sinosoft.cloud.access.net.ClientProperties;
import com.sinosoft.cloud.access.net.NettyProperties;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.annotation.Order;

import java.util.Map;

/**
 * cloud-netty
 *
 * @title: cloud-netty
 * @package: com.sinosoft.cloud.access
 * @author: yangming
 * @date: 2017/12/2 上午11:43
 */
@Configuration
@EnableConfigurationProperties({NettyProperties.class, ClientProperties.class})
public class AccessConfiguration {

    @Autowired
    private NettyProperties nettyProperties;

    public static final String BASE_PACKAGE = "com/sinosoft/cloud/access/";


    @Bean
    public Map map() {

        Map servers = nettyProperties.getServers();
        if (servers != null) {
            for (Object o : servers.keySet()) {
                System.out.println(o + "->" + servers.get(o));
            }
        }

        return servers;
    }

    @Bean
    @Order(1)
    public EventLoopGroup workerGroup() {
        EventLoopGroup workerGroup;
        if (nettyProperties != null && nettyProperties.getWorkerThreads() > 0) {
            workerGroup = new NioEventLoopGroup(nettyProperties.getWorkerThreads());
        } else {
            workerGroup = new NioEventLoopGroup();
        }
        return workerGroup;
    }
}
